<?php

namespace App\Http\Model;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class ContentCheck extends Base
{
    protected $table = 'content_check';
    protected $logTable = 'content_check_log';
    private $contentCheckSwitchKey = 'CONTENT:CHECK:SWITCH';
    private $contentImageQueueKey = 'CONTENT:IMAGE';

    public function __construct(array $attributes = [])
    {
        parent::__construct($this->table, $attributes);
    }

    /*
     * $checkObj
        1|文字-用户昵称
        2|文字-用户签名
        3|文字-房间标题
        4|文字-房间公告
        5|文字-房间简介
        6|语音-我的声音
        7|图片-房间封面
        8|图片-相册/头像
        9|文字-心愿墙心愿名称
        10|文字-心愿墙心愿详情
        11|动态-用户发布动态
        12|动态评论-文字评论
        13|动态评论-语音评论
        26|交友折纸
     * */

    /**
     * 添加记录
     *
     * @param int $userId 用户ID
     * @param int $checkObj 检测对象
     * @return bool
     * */
    public function create($userId, $checkObj = 0, $content = "", $oldContent = "", $dataId = 0, $contentAdd = "", $contentId = 0,$robotCheckStatus = 0)
    {

        if (!in_array($checkObj, [8, 9, 10, 11, 12 ,13])) {
            //查询相同检测对象
            DB::table($this->table)->where([
                "user_id" => $userId,
                "check_obj" => $checkObj,
                "status" => 0,
                "data_id" => $dataId
            ])->update(["is_delete" => 1]);
        }

        $data = [
            'user_id' => $userId,
            'check_obj' => $checkObj,
            'content' => $content,
            'old_content' => $oldContent,
            'data_id' => $dataId,
            'robot_check_status' => $robotCheckStatus,
            'content_add' => $contentAdd,
            'content_id' => $contentId,
            'created_at' => time()
        ];
        return DB::table($this->table)->insertGetId($data);
    }

    public function getByIds($ids)
    {
        $row = DB::table($this->table)
            ->whereIn("id", $ids)
            ->where("is_delete", "=", 0)
            ->get();
        return self::objToArray($row);
    }

    public function updateContentCheck($id, $status, $type, $opt_id, $userId = 0)
    {
        $updateData = [
            "check_status" => $status
        ];

        if ($type == 2) {
            $updateData = [
                "review_check_status" => $status
            ];
        }

        $updateData['status'] = $type;

        DB::table($this->table)
            ->where(["id" => $id])
            ->update($updateData);

        if($type == 2){
            //复审通过之后还可以驳回，二次驳回时更新日志
            $check_data = DB::table($this->logTable)->where(['content_check_id'=>$id ,'check_type'=>2])->first();
            if(!empty($check_data)){
                return DB::table($this->logTable)->where('id' ,$check_data->id)
                    ->update(array('check_status'=>$status));
            }
        }
        $data = [
            'user_id' => $userId,
            'opt_id' => $opt_id,
            'content_check_id' => $id,
            'check_status' => $status,
            'check_type' => $type,
            'created_at' => time()
        ];
        return DB::table($this->logTable)->insertGetId($data);
    }

    public function getContentCheckListByDataIdAndCheckObj($dataId, $checkObj)
    {
        $row = DB::table($this->table)
            ->where("data_id", "=", $dataId)
            ->where("status", "=", 0)
            ->where("is_delete", "=", 0)
            ->whereIn("check_obj", $checkObj)
            ->get();
        return self::objToArray($row);
    }

    public function delContentCheckByCheckObjAndDataId($checkObj, $dataId)
    {
        DB::table($this->table)->where([
            "data_id" => $dataId,
            "check_obj" => $checkObj
        ])->update(["is_delete" => 1]);
    }

    public function delContentCheckByContents($userId, $checkObj, $contents)
    {
        DB::table($this->table)->where([
            "user_id" => $userId,
            "check_obj" => $checkObj
        ])->whereIn('content', $contents)->update(["is_delete" => 1]);
    }

    public function getContentCheckListByUserIdAndCheckObj($userId, $checkObj)
    {
        $row = DB::table($this->table)
            ->where("user_id", "=", $userId)
            ->where("status", "=", 0)
            ->where("is_delete", "=", 0)
            ->whereIn("check_obj", $checkObj)
            ->get();
        return self::objToArray($row);
    }

    public function getContentCheckListByDataIds($dataIds, $checkObj = 0)
    {
        $row = DB::table($this->table)
            ->where("status", "=", 0)
            ->where("is_delete", "=", 0)
            ->whereIn("check_obj", $checkObj)
            ->whereIn("data_id", $dataIds)
            ->get();
        return self::objToArray($row);
    }

    public function getContentCheckListByContentIds($contentIds, $checkObj)
    {
        $row = DB::table($this->table)
            ->where("status", "=", 0)
            ->where("is_delete", "=", 0)
            ->whereIn("check_obj", $checkObj)
            ->whereIn("content_id", $contentIds)
            ->groupBy("content_id")
            ->select('content_id',DB::raw('count(1) as total'))
            ->get();
        return self::objToArray($row);
    }

    public function getContentCheckSwitch(){
        $contentCheckSwitch = Redis::get($this->contentCheckSwitchKey);
        if ($contentCheckSwitch) {
            return true;
        }
        return false;
    }

    /**
     * 图片审核队列
     * @param $userId
     * @param $imgUrl
     * @param $type 1.相册、2.房间、3.动态
     * @return mixed
     */
    public function pushImage($userId, $imgUrl, $type)
    {
        return Redis::LPUSH($this->contentImageQueueKey, "$userId|$imgUrl|$type");
    }


    /**
     * 获取图片审核数据
     * @return array|null
     */
    public function popImage()
    {
        $strItem = Redis::RPOP($this->contentImageQueueKey);
        if (empty($strItem)){
            return null;
        }
        return explode('|', $strItem);
    }


    /**
     * 添加图片检测结果
     * @param $img_url
     * @param $user_id
     * @param $source
     * @param $result
     */
    public function addImgSesult($img_url, $user_id, $source, $result)
    {
        DB::table('content_img_result')->insert([
            'img_url' => $img_url,
            'user_id' => $user_id,
            'source' => $source,
            'result' => intval($result),
            'created_at' => date("Y-m-d H:i:s", time()),
            'updated_at' => date("Y-m-d H:i:s", time())
        ]);
    }

}